<style>
	/* 外观等待改进
	 * 为什么第一行分隔输出？
	 */
	a:link,a:visited,a:hover { text-decoration:none }

	#tag { padding:10px; margin:20px 0; border-radius: 5px; list-style-type: none; }
	#tag:after { content: " "; display: block; height: 0; clear: both; visibility: hidden }
	#tag>li { float: left; border:1px solid #e5e9ef; border-radius:15px; margin:10px 8px 0 0; padding:0 5px 0 3px;}
	#tag>li:hover { border:1px solid #999; }
	#tag>li:last-child { padding:0; width:30px; text-align: center; }
	#tag>li>a { font-size:12px; color: #555; display: block; }
	#tag>li>a:hover { color:#222; }
	#tag>li>a>i { padding:0 3px }

	#tagPush { padding:0px; margin:20px 0; border-radius: 5px; list-style-type: none; }
	#tagPush:after { content: " "; display: block; height: 0; clear: both; visibility: hidden }

	#tagPush>li { float: left; width:25%; border-radius:3px; margin:0; padding:8px; }
	#tagPush>li:hover { background: rgba(50,100,100,.1) }
	#tagPush>li>a { color:#555; display:block; }
	#tagPush>li>a>img { width: 100%; border:none; border-radius:4px; }

	@media screen and (max-device-width: 600px) {
		#tagPush>li { width:50%; }
	}
</style>

<?php
	// 低版本兼容 PHP 5.4
	if(!function_exists('array_column')){
		function array_column($arr,$key){ return array_map(function($val) use ($key){ return $val[$key]; },$arr); }
	}

	// 先读数据 ( 最大只允许添加和读出20个标签 )
	$page = '1';
	$pagesize = '20';
	$list = array();
	$lists = array();
	$arr = db_find('git_tags_thread', array('tid'=>$tid), array(), $page, $pagesize);

	if($arr){
		// 读标签名
		$tagid = array_column($arr, 'tagid');
		$list  = db_find('git_tags', array('tagid'=>$tagid), array(), $page, $pagesize);

		// 关联推荐 (取得所有关联的主题, 可能数量庞大, 暂限制1k)
		$tlist = db_find('git_tags_thread', array('tagid'=>$tagid), array('tid'=>-1), 1 , 1000);
		$tlist = array_column($tlist, 'tid');                        // 取出tid, 可能数量庞大
		$tlist = array_unique($tlist);                               // 移除重复
		$tlist = array_diff($tlist,array('tid'=>$tid));              // 移除自身id

		shuffle($tlist);                                             // 打乱排序(需要保障多标签时不只取前面的)
		$tlist = array_slice($tlist,0,4);                            // 取前 4 个,(可能不足4个)

		if($tlist){
			$lists = db_find('thread', array('tid'=>$tlist));
			//print_r($lists);
		}
	}
?>

<!-- TAG -->
<ul id="tag">
	<?php foreach ( $list as $k ){ ?>
	<li data-tag="<?php echo $k['tagid'] ?>"><a href='<?php echo url("tag-$k[tagid]") ?>'><i class="icon-tag"></i><?php echo $k['name'] ?></a></li>
	<?php } ?>
	<li><a title="添加标签" data-toggle="modal" data-target="#tagRe"><i class="icon-plus"></i></a></li>
</ul>


<!-- 相关推荐 -->
<ul id="tagPush">
	<?php foreach ( $lists as $k ){ ?>
	<li><a href='<?php echo url("thread-$k[tid]") ?>'><img src="plugin/git_tags/view/img/test.gif"><?php echo $k['subject'] ?></a></li>
	<?php } ?>
</ul>


<!-- TAG / 按钮触发模态框 -->
<div class="modal fade" id="tagRe" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h4 class="modal-title" id="myModalLabel">标签添加</h4>
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			</div>
			<div class="modal-body">
				<p>输入标签名</p>
				<form class="bs-example bs-example-form" role="form" >
					<div class="input-group mb-3">
						<div class="input-group-prepend"><span class="input-group-text" id="basic-addon1"><i class="icon-tags"></i></span></div>
						<input type="text" class="form-control" placeholder="" aria-label="" aria-describedby="basic-addon1" id="tagdata">
					</div>
				</form>
				<div class="">
					<a href="#" title="修改记录"><i class="icon-edit"></i></a>
					<a href="#" title="使用说明"><i class="icon-question-circle"></i></a>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-primary" data-tid="<?php echo $tid; ?>" id="tagsadd">添加标签</button>
			</div>
		</div>
	</div>
</div>